/*
 * Copyright (C) 2013 AdminsProL2
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

package com.l2jserver.gameserver.model.actor.instance;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Logger;

import com.l2jserver.Config;
import com.l2jserver.L2DatabaseFactory;
import com.l2jserver.gameserver.datatables.SkillTable;
import com.l2jserver.gameserver.model.skills.L2Skill;

/**
 * @author fernandopm
 * @reworked Fissban
 * @Fuente: http://www.adminsprol2.com/t38190-sistema-vip-by-me
 */

public class L2VipInstance
{
	private static final Logger _log = Logger.getLogger(L2VipInstance.class.getName());
	
	private L2VipInstance()
	{
	}
	
	public boolean leerVip(L2PcInstance player)
	{
		boolean isVip = false;
		
		try (Connection con = L2DatabaseFactory.getInstance().getConnection();
			PreparedStatement statement = con.prepareStatement("SELECT charId FROM vips WHERE charId=?"))
		{
			statement.setInt(1, player.getObjectId());
			ResultSet rset = statement.executeQuery();
			
			isVip = rset.next();
		}
		catch (SQLException sqle)
		{
			_log.severe("Couldnt get vip data from " + player.getName());
			_log.severe(sqle.getMessage());
			return false;
		}
		if (isVip)
		{
			return true;
		}
		return false;
	}
	
	public void addVip(L2PcInstance player)
	{
		updatedb(player, true);
	}
	
	public void removeVip(L2PcInstance player)
	{
		updatedb(player, false);
	}
	
	private void updatedb(L2PcInstance player, boolean add)
	{
		String sql = "";
		if (add)
		{
			sql = "INSERT INTO vips (charId) VALUES (?)";
		}
		else
		{
			sql = "DELETE FROM vips WHERE charId = ?";
		}
		
		try (Connection con = L2DatabaseFactory.getInstance().getConnection();
			PreparedStatement statement = con.prepareStatement(sql);)
		{
			statement.setInt(1, player.getObjectId());
			statement.execute();
		}
		catch (SQLException sqle)
		{
			_log.severe("Couldnt get vip data from " + player.getName());
		}
	}
	
	public void onEnter(L2PcInstance player)
	{
		if (player.isVip())
		{
			if (Config.VIP_COLOR_NAME_TITLE)
			{
				Integer colorName = Integer.decode("0x" + Config.VIPS_NAMECOLOR);
				Integer colorTitle = Integer.decode("0x" + Config.VIPS_TITLECOLOR);
				player.getAppearance().setNameColor(colorName);
				player.getAppearance().setTitleColor(colorTitle);
			}
			
			for (int skill : Config.VIP_SKILLS.keySet())
			{
				L2Skill sk = SkillTable.getInstance().getInfo(skill, Config.VIP_SKILLS.get(skill));
				player.addSkill(sk, false);
			}
		}
	}
	
	public static L2VipInstance getInstance()
	{
		return SingleTonHolder._instance;
	}
	
	@SuppressWarnings("synthetic-access")
	static class SingleTonHolder
	{
		static L2VipInstance _instance = new L2VipInstance();
	}
}